<?php 


class Updater {
	private $root_path;
	private $local_data;
	private $remote_data;
	private $server_url;
	private $local_data_file ;
	private $download_all = false ;
	private $default_server_url = "http://127.0.0.1/test/med-dot-server.php";
	private $version = false;

	public function __construct($options = array()){
		error_reporting(E_ALL);


		$this->root_path = dirname(__FILE__).'/../../..';

		if(empty($options['server_url'])){
			$this->server_url = $this->default_server_url ;
		}else{
			$this->server_url = $options['server_url'];
		}
		$this->local_data_file = dirname(__FILE__).'/../version.serialize';
		if(!file_exists($this->local_data_file)){
			file_put_contents($this->local_data_file , serialize(array('version'=>1)) );
			$this->download_all = true;
		}else{
			$this->local_data = unserialize(file_get_contents($this->local_data_file));
			if(!is_array($this->local_data) || count($this->local_data) == 0 || empty($this->local_data['version'])){
				$this->download_all = true;
			}
		}


		//die(file_get_contents("http://www.google.jo/search?sourceid=chrome&ie=UTF-8&q=file_get_contents+for+remote+url"));

		$this->remote_data = @unserialize(file_get_contents($this->server_url));
		if(!is_array($this->remote_data) || empty($this->remote_data['version'])){
			$this->remote_data = array();
			// to ensure no upgrade is happinging if the server is down
			$this->remote_data['version'] = 0;
			$this->remote_data['affected_version'] = 99999;
			
		}
		//die(var_dump($this->remote_data));

	}

	public function update(){
		if($this->need_update()){

			if(!empty($this->remote_data['files']) && is_array($this->remote_data['files'])){
				$this->_add_files($this->remote_data['files']);
			}
			if(!empty($this->remote_data['del_files']) && is_array($this->remote_data['del_files'])){
				$this->_del_files($this->remote_data['del_files']);
			}
			if(!empty($this->remote_data['sql']) && is_array($this->remote_data['sql'])){
				//var_dump($this->remote_data['sql']);exit
				$this->_sql($this->remote_data['sql']);
			}

			$this->version = $this->remote_data['version'];
			$this->commit();
		}

	}

	private function commit(){
		$arr = array();
		$arr['version'] = $this->getVersion();
		file_put_contents( $this->local_data_file , serialize($arr) );
	}
	private function _add_files($files){
		foreach ($files as $struct){
			$local_path = $struct['local'];
			$remote_path = $struct['remote'];
			$remote_data = file_get_contents($remote_path);
			file_put_contents( $this->_normalize_file_path($local_path) , $remote_data );
		}
	}
	private function _del_files($files){
		foreach ($files as $file_path){
			if(file_exists($this->_normalize_file_path($file_path))){
				unlink($this->_normalize_file_path($file_path));
			}
		}
	}

	private function _sql($sqls){

		include(dirname(__FILE__).'/../config/database.php');
		//var_dump($db);exit;
		$link = mysql_connect($db[$active_group]['hostname'],$db[$active_group]['username'],$db[$active_group]['password']);
		mysql_select_db($db[$active_group]['database']);
		foreach ($sqls as $sql){
			//die($sql);
			mysql_query($sql,$link);
		}
	}

	private function _normalize_file_path($path){

		$path = ltrim($path,'/');
		$path = ltrim($path,'\\');
		return $this->root_path.DIRECTORY_SEPARATOR.$path;
	}
	public function getVersion(){
		return $this->version;
	}

	public function need_update(){
		return 
			floatval($this->local_data['version']) < floatval($this->remote_data['version'])
			&&
			floatval($this->local_data['version']) >= floatval($this->remote_data['affected_version']);
			
	}



}